package cc.ycws.api.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cc.ycws.core.domain.Unitdoor;
import cc.ycws.core.service.UnitdoorService;
import com.alibaba.fastjson.JSON;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ssm/unitdoor")
public class UnitdoorApi {

    @Resource
    UnitdoorService unitdoorService;

    @RequestMapping(value = {"/show"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String show(HttpServletRequest request, Unitdoor unitdoor, ModelMap map) throws Exception {
        String s1 = request.getParameter("address");
        String address = new String(s1.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("--------" + address);
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("address", address);
        maps.put("number", "all");
        List<Unitdoor> list = unitdoorService.showAll(maps);
        return JSON.toJSONString(list);
    }

    @RequestMapping(value = "/showAll")
    public String showAll(ModelMap map) {
        List<Unitdoor> unitdoor = unitdoorService.showUnitdoor();
        map.addAttribute("unitdoor", unitdoor);
        return "Unitdoor";
    }

    @RequestMapping(value = "/selectOne")
    public String selectOne(ModelMap map, String id) {
        Unitdoor unitdoor = unitdoorService.selectOne(id);
        map.addAttribute("unitdoor", unitdoor);
        return "showUnitdoor";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Unitdoor unitdoor) {
        unitdoor.setYid(unitdoor.getYid());
        unitdoor.setNumber(unitdoor.getNumber());
        unitdoor.setAddress(unitdoor.getAddress());
        unitdoor.setIdnumber(unitdoor.getIdnumber());
        unitdoorService.update(unitdoor);
        return "redirect:/unitdoor/showAll";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(Unitdoor unitdoor) {
        unitdoor.setNumber(unitdoor.getNumber());
        unitdoor.setAddress(unitdoor.getAddress());
        unitdoor.setIdnumber(unitdoor.getIdnumber());
        unitdoorService.insert(unitdoor);
        return "redirect:/unitdoor/showAll";
    }
}

